package uk.co.q3c.deplan.server.unit.domain.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.domain.resource.CalendarAdjustment;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.client.domain.util.CalendarAdjustments;
import uk.co.q3c.deplan.server.unit.dao.LocalDatastoreTestCase;
import uk.co.q3c.deplan.server.unit.dao.TestContainer;
import uk.co.q3c.deplan.server.unit.dao.TestDao;

import com.googlecode.objectify.Key;

/**
 * {@link CalendarAdjustments}
 * 
 * @author DSowerby 26 Mar 2009
 * 
 */
@Test
public class CalendarAdjustments_UT extends LocalDatastoreTestCase {
	transient protected final Logger logger = Logger.getLogger(getClass().getName());
	CalendarAdjustments adjustments;
	SimpleDateFormat sdf = new SimpleDateFormat();

	@Override
	protected void beforeMethod() throws Exception {
		super.beforeMethod();
		adjustments = new CalendarAdjustments(connection);
	}

	@SuppressWarnings("unchecked")
	public void doit() throws ParseException, PersistenceReferenceException {
		Calendar cal = new GregorianCalendar();
		cal.set(2008, 2, 15);
		CalendarAdjustment cadj = new CalendarAdjustment();
		cadj.setAdjustment(10);
		String key = adjustments.add(cal.getTime(), cadj);
		Assert.assertEquals(key, "20080315");
		CalendarAdjustment obj = adjustments.get(key);
		Assert.assertEquals(obj.getAdjustment(), 10);
		Assert.assertEquals(adjustments.size(), 1);
		// add a second entry on the same date
		// displaces the existing entry
		cadj = new CalendarAdjustment();
		cadj.setAdjustment(9);
		adjustments.add(cal.getTime(), cadj);
		obj = adjustments.get(key);
		Assert.assertEquals(obj.getAdjustment(), 9);
		Assert.assertEquals(adjustments.size(), 1);

		// Dao testdao;
		connection.openSession();
		connection.calendarService()
				.save(adjustments);
		connection.closeSession();
		Assert.assertEquals(1, connection.calendarService()
				.countCalendarAdjustments());

		Key<CalendarAdjustments> k = (Key<CalendarAdjustments>) adjustments.getKey();
		connection.openSession();

		CalendarAdjustments result = testdao.getObjectFromKey(k);
		Assert.assertEquals(result.size(), 1);
		cadj = result.get(key);
		logger.info(cadj);
		Assert.assertEquals(cadj.getAdjustment(), 9);
		adjustments.remove(key);
		Assert.assertEquals(adjustments.size(), 0);
		connection.closeSession();
	}

	public void doit2() throws ParseException {
		TestContainer tc = new TestContainer(connection);
		adjustments = tc.getAdjustments();
		Calendar cal = new GregorianCalendar();
		cal.set(2008, 2, 15);
		CalendarAdjustment cadj = new CalendarAdjustment();
		cadj.setAdjustment(10);
		String key = adjustments.add(cal.getTime(), cadj);
		Assert.assertEquals(key, "20080315");
		CalendarAdjustment obj = adjustments.get(key);
		Assert.assertEquals(obj.getAdjustment(), 10);
		Assert.assertEquals(adjustments.size(), 1);
		// add a second entry on the same date
		// displaces the existing entry
		cadj = new CalendarAdjustment();
		cadj.setAdjustment(9);
		adjustments.add(cal.getTime(), cadj);
		obj = adjustments.get(key);
		Assert.assertEquals(obj.getAdjustment(), 9);
		Assert.assertEquals(adjustments.size(), 1);

		tc.save();

		TestDao testdao = new TestDao(connection);
		List<TestContainer> tcresults = testdao.testContainers();
		Assert.assertEquals(tcresults.size(), 1);
		CalendarAdjustments result = tcresults.get(0)
				.getAdjustments();
		Assert.assertEquals(result.size(), 1);
		cadj = result.get(key);
		logger.info(cadj);
		Assert.assertEquals(cadj.getAdjustment(), 9);
		adjustments.remove(key);
		Assert.assertEquals(adjustments.size(), 0);

	}

}
